/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testingP;

import javax.persistence.EntityManager;
import javax.swing.JFrame;
import model.AppUser;


import avenue.MyWindowEvent;
import avenue.Session;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import model.AppUser;
import model.DBManager;

import model.Marka;
import java.util.*;
/**
 *
 * @author admin
 */
public class jf3 extends javax.swing.JFrame
{

    //AppUser u;
    EntityManager em;
    JFrame thisFrame;
    int s;
    Marka m;
    /**
     * Creates new form jf3
     */
    public jf3()
    {

        initMyC();
        
        initComponents();
    }

    
    
    public void initMyC()
    {
       em = DBManager.em;
       if ( !em.getTransaction().isActive())
         {
         em.getTransaction().begin();
         }   
       
      
      
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        markaQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("select mar from Marka mar");
        markaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(markaQuery.getResultList());
        saveButton = new javax.swing.JButton();
        newRowButton = new javax.swing.JButton();
        updateButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        saveButton.setText("save");
        saveButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                saveButtonActionPerformed(evt);
            }
        });

        newRowButton.setText("addRow");
        newRowButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                newRowButtonActionPerformed(evt);
            }
        });

        updateButton.setText("update");
        updateButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                updateButtonActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, markaList, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nameMarkas}"));
        columnBinding.setColumnName("Name Markas");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();

        jScrollPane2.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(saveButton)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newRowButton)
                        .addGap(44, 44, 44)
                        .addComponent(updateButton))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(157, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(142, 142, 142)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newRowButton)
                    .addComponent(updateButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(saveButton))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveButtonActionPerformed
    {//GEN-HEADEREND:event_saveButtonActionPerformed
        // TODO add your handling code here:
        
        
        
     

//     
//      em.refresh(markaList);
//       em.getTransaction().commit();
//       
       
       //int row = markaList.size() - 1; 
        
        
        
        
        
        
        ///////////////////////////////////////////////////
//     if ( !em.getTransaction().isActive())
//         {  em.getTransaction().begin();  } 
        
//        u = new AppUser();
//      em.persist(u);
//        appuser_list.add(u);
//       em.getTransaction().commit();
       
        //userTable.getModel().addTableModelListener(userTable);  
        
        //DefaultTableModel tm =(DefaultTableModel)userTable.getModel();  
        
       // TableModel tm2 = userTable.getModel();
        
        //TableModel model=userTable.getModel();  
        //model.addRow(new Object []{ });
        
        
//         userTable.getModel().addTableModelListener(userTable);  
       //     TableModel tm2 = userTable.getModel();
           // tm2.add
       
       
    }//GEN-LAST:event_saveButtonActionPerformed

    private void newRowButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_newRowButtonActionPerformed
    {//GEN-HEADEREND:event_newRowButtonActionPerformed
        // TODO add your handling code here:
        m = new Marka();
        em.persist(m);
        markaList.add(m);
 
        // prosthetei grami sto table(mono afti i grami)
    }//GEN-LAST:event_newRowButtonActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_updateButtonActionPerformed
    {//GEN-HEADEREND:event_updateButtonActionPerformed

 if ( jTable1.isEditing() )
        {
            jTable1.getCellEditor().stopCellEditing();
        }
 
        if ( em.getTransaction().isActive()){
            em.getTransaction().commit();
        }
        
      //  this.dispose();
        
        
        

    }//GEN-LAST:event_updateButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
          {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
              {
                if ("Nimbus".equals(info.getName()))
                  {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                  }
              }
          } catch (ClassNotFoundException ex)
          {
            java.util.logging.Logger.getLogger(jf3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex)
          {
            java.util.logging.Logger.getLogger(jf3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex)
          {
            java.util.logging.Logger.getLogger(jf3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex)
          {
            java.util.logging.Logger.getLogger(jf3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new jf3().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private java.util.List<Marka> markaList;
    private javax.persistence.Query markaQuery;
    private javax.swing.JButton newRowButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton updateButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
